<%@page import="com.lianjing.sh.spring.web.controller.enhance.operate.Util_Operate"%>
<%@page import="com.lianjing.sh.spring.web.controller.base.operate.OperateTarget"%>
<%@page import="com.lianjing.sh.spring.web.controller.base.operate.OperatableActionBean"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Collection"%>
<%@page import="com.sh_lianjing.web.el.EL"%>
<%@page import="java.util.Date"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<script type='text/javascript' src='/webjars/bootstrap-validator/js/validator.js'></script>

<c:set var="bean" value="${moduleBody.data}" scope="request" />
<!-- S 表单组 -->
<% OperatableActionBean defaultOperate = EL.eval("${moduleBody.defaultOperate}", OperatableActionBean.class, pageContext); %>
<form class="myDataSaveOrModify" role="form" 
 bindBeforeAction="@fun:fun_popup_confirm('确认操作?')"
 bindAfterAction="@fun:fun_bindAfterAction('<%=OperateTarget.ReferrerOperateTargetParaName%>', '${moduleBody.defaultOperate.actionType}', '')"
 com="${moduleBody.type=='Add' ? 'myDataSave' : 'myDataUpdate'}"
 access="${moduleBody.defaultOperate.url}" 
 bindAfterTo='${RootUrl}<%=Util_Operate.returnTo(defaultOperate, request)%>'
>
  	<%-- 零时解决路径变量自动为{id}赋值问题 --%>
 	<!-- <input name="id" type="hidden" value="-1"/> -->
	
	<%-- token --%>
	<input type="hidden" name="token" value="${token}" >
	<% Object bean = EL.eval("${bean}", Object.class, pageContext); %>
	<% Boolean isList = null!=bean && bean instanceof List; %>
	<% if(isList){ %>
		<c:forEach items="${bean}" var="item" varStatus="status" >
			<c:set var="beanItem" value="${item}" scope="request"/>
			<%-- 输入框 --%>
			<fieldset style="border: medium; solid;" >
				<c:if test="${!empty moduleBody.modelInfo.titleMethodInfo}">
     				<legend>${beanItem[moduleBody.modelInfo.titleMethodInfo.info.fieldName]}</legend>
     			</c:if>
				<c:forEach items="${moduleBody.modelInfo.methodInfos}" var="methodInfo" >
		     		<c:set var="formInfo" value="${methodInfo.info}" scope="request"/>
		     		<c:set var="baseFieldName" value="models[${status.index}]." scope="request"/>
		     		<c:if test="${formInfo.display!='None'}">
		     			<%-- 普通模型 --%>
		     			<c:if test="${formInfo.display=='Hide'}"> <div style="display: none;"> </c:if>
		     				<jsp:include page="/WEB-INF/jsp/_admin/common/module/saveOrModify/inputItem_version_1.jsp" />
		     			<c:if test="${formInfo.display=='Hide'}"> </div> </c:if>
			   		</c:if>
				</c:forEach>
			</fieldset>
		</c:forEach>
	<%}else { %>
		<%-- 输入框 --%>
		<c:set var="beanItem" value="${bean}" scope="request"/>
	    <c:forEach items="${moduleBody.modelInfo.methodInfos}" var="methodInfo" >
	    	<c:set var="isGroup" value="${!empty methodInfo.group}" scope="request"/>
	     	<%-- 非分组 --%>
	     	<c:if test='${!isGroup}'>
	     		<c:set var="formInfo" value="${methodInfo.info}" scope="request"/>
	     		<c:set var="baseFieldName" value="" scope="request"/>
	     		<c:if test="${formInfo.display!='None'}">
	     			<%-- 普通模型 --%>
	     			<c:if test="${formInfo.display=='Hide'}"> <div style="display: none;"> </c:if>
	     				<jsp:include page="/WEB-INF/jsp/_admin/common/module/saveOrModify/inputItem_version_1.jsp" />
     				<c:if test="${formInfo.display=='Hide'}"> </div> </c:if>
		   		</c:if>
	       	</c:if>
	       	
	       	<%-- 分组 --%>
	       	<c:if test="${isGroup}">
	       		<!-- 组名 -->
	       		<%-- <c:set value="${formInfo[0]}" var="firstItem" />
	       		<div class="form-group has-feedback text-info col-lg-6" >
	       		<label class="control-label col-lg-2" for="${fieldName}" style="color:red;">
					${firstItem.group.comment}
				</label>
	       		</div> --%>
	       		<!-- 组 -->
	       		<div class="col-md-12 xxh_linkage">
	       			<label>省市县三级联动:</label>
	       			<div>
				       	<c:forEach items="${methodInfo.group}" var="formInfo">
				       		<c:if test="${formInfo.display!='None'}">
				       			<c:if test="${formInfo.display=='Hide'}"> <div style="display: none;"> </c:if>
						       		<c:set var="baseFieldName" value="" scope="request"/>
						       		<c:set var="formInfo" value="${formInfo}" scope="request" />
					     			<jsp:include page="/WEB-INF/jsp/_admin/common/module/saveOrModify/inputItem_version_1.jsp" />
					     			<c:set var="formInfo" value="${methodInfo.info}" scope="request"/>
				     			<c:if test="${formInfo.display=='Hide'}"> </div> </c:if>
			     			</c:if>
				       	</c:forEach>
			       	</div>
		       	</div>
	       	</c:if>
		</c:forEach>
	<%} %>
     		
	<!-- S 提交 -->
	<div class="form-group col-lg-12">
	    <div class="col-lg-offset-5 col-lg-9">
	        <button class="btn btn-danger js_submit" >提交</button>
	        <a class="btn btn-success js_forEmbedPopup_cancel" href="${RootUrl}${navigationContext.preInfo.url}" >取消</a>
	    </div>
	</div>
	<!-- E 提交 -->
</form>
<!-- E 表单组 -->

<script type="text/javascript">
	/**
	 * 开启tooltip显示验证信息
	 */
	$("[data-toggle='tooltip']")
		.attr("data-placement", "top")
		.tooltip();
	/*
	 * 覆盖bootstrap-validator插件的默认方法
	 */
	var validator = $.fn.validator.Constructor;
	var showErrors = validator.prototype.showErrors = function ($el) {
	    var errors = $el.data('bs.validator.errors')
	    var $group = $el.closest('.form-group')
	    var $tooltip = $group.find("[data-toggle='tooltip']")
	    var $feedback = $group.find('.form-control-feedback')

	    if (!errors.length) return
	    // tooltip
	    $tooltip.data('bs.validator.originalContent') === undefined && $tooltip.data('bs.validator.originalContent', $tooltip.attr("data-original-title"))
	    $tooltip.attr("data-original-title", errors).tooltip('show');
	    
	    $group.addClass('has-error has-danger')
	    $group.hasClass('has-feedback')
	      && $feedback.removeClass(validator.DEFAULTS.feedback.success)
	      && $feedback.addClass(validator.DEFAULTS.feedback.error)
	      && $group.removeClass('has-success')
	}
	var clearErrors = validator.prototype.clearErrors = function ($el) {
	    var $group = $el.closest('.form-group')
	    var $tooltip = $group.find("[data-toggle='tooltip']")
	    var $feedback = $group.find('.form-control-feedback')
		// tooltip
		var $comEditor = $group.findCom('myEditer');	//为百度编辑器判断
		if($el.is(":focus") || $comEditor.length>0 && $comEditor.data("editor") && $comEditor.data("editor").selection.isFocus()){
			$tooltip.attr("data-original-title", $tooltip.data('bs.validator.originalContent')).tooltip('show');
		}
	    
	    $group.removeClass('has-error has-danger has-success')
	    $group.hasClass('has-feedback')
	      && $feedback.removeClass(validator.DEFAULTS.feedback.error)
	      && $feedback.removeClass(validator.DEFAULTS.feedback.success)
	      && getValue($el)
	      && $feedback.addClass(validator.DEFAULTS.feedback.success)
	      && $group.addClass('has-success')
	}
	function getValue($el) {
	    return $el.is('[type="checkbox"]') ? $el.prop('checked')                                     :
	           $el.is('[type="radio"]')    ? !!$('[name="' + $el.attr('name') + '"]:checked').length :
	                                         $el.val()
	  }
	/*
	 * 提交验证处理
	 */
	$(function(){
		$('.myDataSaveOrModify').validator({}).off('submit').on('submit', function (e) {
			if (e.isDefaultPrevented()) {
				$(this).fireBind('bind');
			} else {
				e.preventDefault();
				$(this).fireBind('bind');
			}
		})
	});
	/* 验证失败 */
	function fun_validateFail(){
		var $myValidate = $(this);
		$myValidate
			.data('bs.validator.errors', $myValidate.outputData());
		showErrors($(this));
	}
	/* 验证成功 */
	function fun_validateSuccess(){
		clearErrors($(this));
	}
	/*
	 * 百度编辑器配置
	 */
	var editer_config = {toolbars:[
           [ 'fullscreen',
               'bold', 'italic', 'underline', 'strikethrough', '|', 
               'insertorderedlist', 'insertunorderedlist', '|',
               'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
               'fontfamily', 'fontsize', '|',
               'indent', '|',
               'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify']
       ]}
	var editer_config2 = {
		toolbars:[["source","undo","redo","insertunorderedlist","insertorderedlist","unlink","link","cleardoc","selectall","preview","|","fullscreen","simpleupload","insertimage","emotion","date","bold","italic","underline","fontborder","strikethrough","forecolor","backcolor","superscript","subscript","justifyleft","justifycenter","justifyright","justifyjustify","removeformat","fontfamily","fontsize"]]
	}
	/*
	 * 文件上传插件配置
	 */
	fileuploadConfig();
</script>

<%-- 分组级联使用 --%>
<script>
	$(function(){
		$(".myDataSaveOrModify")
		.find(":input").not('[disabledFlag]').each(function(){
			var $this = $(this)
			if($this.val()!==''){
				$this.prop("disabled", false);
			}
		}).end()
		.findCom("myEditer").each(function(){
			var $this = $(this)
			if($this.val()!==''){
				$this
					.data("editor")
					.setEnabled();
			}
		});
	})
	//
	function js_fun_select_after(){
		var $nextGroup = $(this).closest("[groupName]").next();
		$nextGroup
			.find(":input")
			.prop("disabled", false);
		var editor = $nextGroup
			.findCom("myEditer")
			.data("editor");
		editor && editor.setEnabled();
	}
	function js_fun_select_before(){
		var $group = $(this).closest("[groupName]");
		var $firer = $(this).getFirer("bind");
		if($firer.val()===''){
			var $nextAllGroup = $(this)
				.closest("[groupName]")
				.nextAll("[groupName='"+$group.attr("groupName")+"']");
			
			$nextAllGroup
				.find(":input")
				.prop("disabled", true)
			.end()
				.find("select")
				.empty()
				.append("<option value='' >未选择</option>");
			
			var editor = $nextAllGroup
				.findCom("myEditer")
				.data("editor");
			editor && editor.setDisabled();
			return false;
		}
	}
</script>

<%-- input重定向操作 --%>
<script>
	$(function(){
		$(".myDataSaveOrModify")
			.find(":input").each(function(){
				var $this = $(this);
				var redirect_url = $this.attr("redirect_url");
				var redirect_withParam = $this.attr("redirect_withParam");
				/*
				 * url 不为空则执行重定向操作
				 */
				if(null != redirect_url){
					var formType = $this.attr("formType")
					// select 情况处理
					if(formType == 'Enum'){
						$this.on("change", function(){
							var url = UUrl(redirect_url);
							if('true'==redirect_withParam){
								url.addPara($this.attr("name"), $this.val());
							}
							window.location.href = url.get();
						});
					}
				}
			});
	})
</script>

<!-- 富文本编辑器 -->
<script src="${RootUrl}/ueditor/ueditor.config.js"></script>
<script src="${RootUrl}/ueditor/ueditor.all.min.js"></script>